import React, { useEffect, useState } from 'react'
import Home from '../../views/Sanbox/Home/home'
import UserList from '../../views/Sanbox/User-manage/UserList'
import RoleList from '../../views/Sanbox/right-manage/RoleList'
import RightList from '../../views/Sanbox/right-manage/RightList'
import Nopermission from '../../views/Sanbox/Nopermission/Nopermission'

import NewsAdd from '../../views/Sanbox/news-manage/NewAdd'
import NewsDraft from '../../views/Sanbox/news-manage/NewDraft'
import NewsCategory from '../../views/Sanbox/news-manage/NewsCategory'
import NewsPreview from '../../views/Sanbox/news-manage/NewPreview'
import NewsUpdate from '../../views/Sanbox/news-manage/NewUpdate'
import Audit from '../../views/Sanbox/audit-manage/Audit'
import AuditList from '../../views/Sanbox/audit-manage/AuditList'

import Unpublished from '../../views/Sanbox/publish-manage/Unpublished'
import Published from '../../views/Sanbox/publish-manage/Published'
import Sunset from '../../views/Sanbox/publish-manage/Sunset'
import { Redirect, Route, Switch } from 'react-router-dom'
import axios from 'axios'
import { Spin } from 'antd'
import { connect } from 'react-redux'
function NewsRouter(props) {

    const LocalRouterList = {
        '/home': Home,
        '/user-manage/list': UserList,
        '/right-manage/role/list': RoleList,
        '/right-manage/right/list': RightList,
        '/news-manage/add': NewsAdd,
        '/news-manage/draft': NewsDraft,
        '/news-manage/category': NewsCategory,
        '/news-manage/preview/:id': NewsPreview,
        '/news-manage/update/:id': NewsUpdate,
        '/audit-manage/audit': Audit,
        '/audit-manage/list': AuditList,
        '/publish-manage/unpublished': Unpublished,
        '/publish-manage/published': Published,
        "/publish-manage/sunset": Sunset
    }
    const [BackRouteList, setBackRouteList] = useState([])
    const { role: { rights } } = JSON.parse(localStorage.getItem('token'))

    useEffect(() => {
        Promise.all([
            axios.get('/rights'),
            axios.get('/children'),
        ]).then(res => {
            setBackRouteList([...res[0].data, ...res[1].data])
        })
    }, [])
    const checkRoute = (item) => {
        return LocalRouterList[item.key] && (item.pagepermisson || item.routepermisson)
    }

    const checkUserPermission = (item) => {
        return rights.checked.includes(item)
    }
    return (
        <Spin size='large' spinning={props.loading}>
            <Switch>
                {
                    BackRouteList.map((item) => {
                        if (checkRoute(item) && checkUserPermission(item.key)) {
                            return <Route path={item.key} key={item.key} component={LocalRouterList[item.key]} exact></Route>
                        }
                        return null

                    }

                    )

                }

                <Redirect from='/' to='/home' exact></Redirect>
                {
                    BackRouteList.length > 0 && <Route path='*' component={Nopermission}></Route>
                }
            </Switch>
        </Spin>
    )
}

const mapStateToProps = ({ loadingSet: { loading } }) => {
    return {
        loading
    }
}

export default connect(mapStateToProps)(NewsRouter)